const VueLoaderPlugin = require('vue-loader/lib/plugin')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const devServer = require('./devServer')
module.exports = {
  mode: 'development',
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, '../', '../', 'dist'),
    filename: '[name].[hash:5].js',
    clean: true,
    publicPath: 'auto',
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1, //遇到!import回滚一个loader
              esModule: false,
            },
          },
          'postcss-loader',
        ],
      },
      {
        test: /\.s[ac]ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: ['postcss-preset-env'],
              },
            },
          },
          'sass-loader',
        ],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
      {
        test: /\.js(x)$/i,
        loader: 'babel-loader',
        exclude: '/node_modules',
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      title: 'webpack配置vue',
    }),
    new VueLoaderPlugin(), //为了找到vue
    new MiniCssExtractPlugin({
      filename: '[name].[chunkhash:5].css',
    }),
  ],
  devtool: 'eval-cheap-source-map',
  devServer,
  resolve: {
    alias: {
      '@': path.resolve(__dirname, '../', '../', 'src'), // 由于修改了webpack路径，所以这个也要修改
    },
    extensions: ['.js', '.json', '.vue'], //引入文件的时候省略后缀
  },
  optimization: {
    minimizer: [new CssMinimizerPlugin()],
  },
}
